package com.dgut.legalservice.service.impl;

import com.dgut.legalservice.dao.IOrderDao;
import com.dgut.legalservice.domain.Approval;
import com.dgut.legalservice.domain.Order;
import com.dgut.legalservice.domain.OrderPr;
import com.dgut.legalservice.domain.OrderVo;
import com.dgut.legalservice.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private IOrderDao orderDao;

    @Override
    public List<Integer> addOrder(Order order, String productId) {
        int i = orderDao.addOrder(order);

        int parseInt = Integer.parseInt(productId);
        OrderPr orderPr = new OrderPr();
        orderPr.setOrderId(order.getId());
        orderPr.setProductId(parseInt);
        int j = orderDao.addOrderRer(orderPr);
        int z = orderPr.getId();
        ArrayList<Integer> list = new ArrayList<>();
        list.add(i);
        list.add(z);
        return list;
    }

    @Override
    public List<Order> selectOrders(OrderVo orderVo) {
        return orderDao.selectOrder(orderVo);
    }

    @Override
    public int payFir(Integer id,String payStatus) {
        return orderDao.payFir(id,payStatus);
    }

    @Override
    public int setFirStatus(Order order, Integer orderId) {
        Example example = new Example(Order.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",orderId);
        List<Order> orders = orderDao.selectByExample(example);
        int i;
        if (orders.size()==0){
            i = orderDao.insertSelective(order);
            return order.getId();
        }else {
            Example example1 = new Example(Order.class);
            Example.Criteria criteria1 = example1.createCriteria();
            criteria1.andEqualTo("id",orderId);
            i = orderDao.updateByExampleSelective(order,example1);
            return i;
        }

    }

    @Override
    public int deleteOrder(Integer orderId) {
        int i = orderDao.deleteByPrimaryKey(orderId);

        return i;
    }
}
